/*
 * StringParameterDeployer.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.core.annotations;

import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.EnumMemberValue;
import javassist.bytecode.annotation.IntegerMemberValue;
import javassist.bytecode.annotation.StringMemberValue;

import org.eu4ria.enums.RegexPatterns;
import org.eu4ria.parameters.StringParameterValidator;

public class StringParameterDeployer {

	private RegexPatterns acceptPattern = RegexPatterns.ANY;

	private String exceptChars = "";

	private String customPattern = "";

	private int minlength = 0;

	private int maxlength = Integer.MAX_VALUE;

	public StringParameterDeployer() {

	}

	public StringParameterValidator deploy(Annotation annotation) {

		StringParameterValidator parameter = new StringParameterValidator();
		if (annotation == null) {
			parameter.setAcceptedPattern(this.acceptPattern);
			parameter.setCustomPattern(this.customPattern);
			parameter.setExceptedChars(this.exceptChars);
			parameter.setMaxlength(this.maxlength);
			parameter.setMinlength(this.minlength);
		} else {

			StringMemberValue exceptChars = (StringMemberValue) annotation
					.getMemberValue("exceptChars");
			if (exceptChars != null) {
				parameter.setExceptedChars(exceptChars.getValue());
			} else {
				parameter.setExceptedChars(this.exceptChars);
			}
			StringMemberValue customPattern = (StringMemberValue) annotation
					.getMemberValue("customPattern");
			if (customPattern != null) {
				parameter.setCustomPattern(customPattern.getValue());
			} else {
				parameter.setCustomPattern(this.customPattern);
			}
			IntegerMemberValue minlength = (IntegerMemberValue) annotation
					.getMemberValue("minlength");
			IntegerMemberValue maxlength = (IntegerMemberValue) annotation
					.getMemberValue("maxlength");
			if (minlength != null) {
				parameter.setMinlength(minlength.getValue());
			} else {
				parameter.setMinlength(this.minlength);
			}
			if (maxlength != null) {
				parameter.setMaxlength(maxlength.getValue());
			} else {
				parameter.setMaxlength(this.maxlength);
			}

			EnumMemberValue acceptPattern = (EnumMemberValue) annotation
					.getMemberValue("acceptPattern");
			if (acceptPattern != null) {
				parameter.setAcceptedPattern(Enum.valueOf(RegexPatterns.class,
						acceptPattern.getValue()));
			} else {
				parameter.setAcceptedPattern(this.acceptPattern);
			}
		}
		parameter.compilePattern();
		return parameter;
	}

}
